package com.hj.dapp.business.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hj.dapp.business.menu.dto.BindMenuDto;
import com.hj.dapp.business.role.mapper.RoleMapper;
import com.hj.dapp.business.role.model.Role;
import com.hj.dapp.business.role.model.RoleMenu;
import com.hj.dapp.business.role.service.RoleMenuService;
import com.hj.dapp.business.role.service.RoleService;
import com.hj.dapp.common.constant.CustomerConstant;
import com.hj.dapp.common.request.PageRequest;
import com.hj.dapp.common.resp.RestResp;
import com.hj.dapp.common.util.ExpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import static com.hj.dapp.common.constant.DictEnum.DELETE_FLAG;
import static com.hj.dapp.common.constant.DictEnum.NORMAL_FLAG;
import static com.hj.dapp.config.exp.BusinessErrorEnum.ADMIN_REJECT;
import static com.hj.dapp.config.exp.BusinessErrorEnum.ROLE_DELETE_REJECT;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2023/12/29
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuService roleMenuService;
    @Override
    public List<Role> getRoleListByUser(Long userId) {
        return roleMapper.getRoleListByUserId(userId);
    }

    @Override
    public List<Role> getRoleListByUser(String username) {
        return roleMapper.getRoleListByUser(username);
    }

    @Override
    public List<Role> getRoleList() {
        return roleMapper.selectList(null);
    }

    @Override
    public void insertRole(Role role, Principal principal) {
        role.setCreateTime(LocalDateTime.now());
        role.setCreateBy(principal.getName());
        role.setDelFlag(NORMAL_FLAG.getValue());
        roleMapper.insert(role);
    }

    @Override
    public void updateRole(Role role, Principal principal) {
        role.setLastUpdateTime(LocalDateTime.now());
        role.setLastUpdateBy(principal.getName());
        roleMapper.updateById(role);
    }

    @Override
    public void deleteRole(Long id) {
        if (CustomerConstant.DESIGNER_ROLE_ID.equals(id)
                ||CustomerConstant.REVIEWER_ROLE_ID.equals(id)
                ||CustomerConstant.SUPERVISOR_ROLE_ID.equals(id)
        ) {
            ExpUtil.throwExp(ROLE_DELETE_REJECT);
        }
        Role role = roleMapper.selectById(id);
        role.setDelFlag(DELETE_FLAG.getValue());
        roleMapper.updateById(role);
    }

    @Override
    public void bindMenu(BindMenuDto bindMenuDto, Principal principal) {
        //删除旧的绑定关系
        QueryWrapper<RoleMenu> query = new QueryWrapper<>();
        query.eq("role_id", bindMenuDto.getRoleId());
        roleMenuService.remove(query);
        //绑定roleId和menuId
        List<RoleMenu> list =  bindMenuDto.getMenuIds().stream().map(n -> {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setCreateBy(principal.getName());
            roleMenu.setCreateTime(LocalDateTime.now());
            roleMenu.setRoleId(bindMenuDto.getRoleId());
            roleMenu.setMenuId(n);
            return roleMenu;
        }).collect(Collectors.toList());
        roleMenuService.saveBatch(list);
    }

    @Override
    public IPage<Role> page(PageRequest<Role> pageRequest) {
        Page<Role> page = new Page<>();
        page.setSize(pageRequest.getSize());
        page.setCurrent(pageRequest.getCurrent());
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(pageRequest.getQuery())) {
            if (!ObjectUtils.isEmpty(pageRequest.getQuery().getExpr())) {
                queryWrapper.like("expr", pageRequest.getQuery().getExpr());
            }
            if (!ObjectUtils.isEmpty(pageRequest.getQuery().getName())) {
                queryWrapper.like("name", pageRequest.getQuery().getName());
            }
        }
        queryWrapper.eq("del_flag", NORMAL_FLAG);
        return roleMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Role get(Long id) {
        return roleMapper.selectById(id);
    }

    @Override
    public void edit(Role role, Principal principal) {
        role.setLastUpdateBy(principal.getName());
        role.setLastUpdateTime(LocalDateTime.now());
        roleMapper.updateById(role);

    }
}
